﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ArmoryData;

namespace ArmoryViewerMobile
{
    public partial class PreferencesForm : Form
    {
        public PreferencesForm()
        {
            InitializeComponent();
            this.MinimizeBox = false;
        }

        private void menuCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void PreferencesForm_Load(object sender, EventArgs e)
        {
            Program.Settings.Load();
            txtArmoryUrl.Text = Program.Settings.ArmoryUrl;
            chkConnectOnStartup.Checked = Program.Settings.ConnectOnStartup;
        }

        private void menuSave_Click(object sender, EventArgs e)
        {
            String oldArmoryUrl = Program.Settings.ArmoryUrl;

            //Remove Trailing slash
            if (txtArmoryUrl.Text[txtArmoryUrl.Text.Length - 1] == '/') { txtArmoryUrl.Text = txtArmoryUrl.Text.Remove(txtArmoryUrl.Text.Length - 1, 1); }

            //Save Settings
            Program.Settings.ArmoryUrl = txtArmoryUrl.Text;
            Program.Settings.ConnectOnStartup = chkConnectOnStartup.Checked;
            Program.Settings.Save();

            //Establish Connection to Server if Url Changed
            if (oldArmoryUrl != Program.Settings.ArmoryUrl && Program.Settings.ConnectOnStartup)
            {
                Hide();
                Cursor.Current = Cursors.WaitCursor;
                Connector.GetHtml(new Uri(Program.Settings.ArmoryUrl));
                Cursor.Current = Cursors.Default;
            }

            Close();
        }
    }
}